<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $arr['price'] = $_POST['price'];
    $arr['quantity'] = $_POST['quantity'];
    $arr['id'] = $_POST['id'];
    $arr['name'] = $_POST['name'];
    $arr['option']= $_POST['option'];
    $arr['picture']= $_POST['picture'];
    if(empty($_SESSION["cart"]))
    {
        //1.购物车是空的，第一次点击添加购物车
        $_SESSION["cart"]=[];
        $_SESSION["cart"][]=$arr;
    }else{
        $session_arr=$_SESSION["cart"];
        $session_arr[]=$arr;
        $_SESSION["cart"]=$session_arr;
    }
    header("location:cart.php");
}

require_once('tools.php');
top_module('Product');
?>
<html>
    <link id='stylecss' type="text/css" rel="stylesheet" href="css/singleProduct.css">

<?php
    $id = $_GET['id'];
    function idexists($id){
        
        if($id >= 1 & $id <= 4){
                
                return true;
            }else  {
                return false;
        }
        
    }
    
    if(isset($_GET['id']) && idexists($_GET['id'])){
        
        $file=fopen($filename='products.txt', 'r');
    
        
    
        $headings = fgetcsv($file, 0, "\t");
    
        while ($aLineOfCells = fgetcsv($file, 0, "\t")) {
            $products = $aLineOfCells;

            if($products[0] == $id){
            
                $name = $products[1];
            
                $description = $products[2];
                $option1 = $products[3];
                $option2 = $products[4];
                $op1Price =$products[5];
                $op2Price =$products[6];
                $picture = $products[7];
            }
        
        }

        fclose($file);
        
    }else{
        
        header("Location: products.php");
        
    }
    
      
?>
<?php

$form = <<<"OUTPUT"

    <script>
        
    
        //form validation function, called when submit button is clicked 
        function validateForm() {

            //get current quantity number
            var quantity = document.getElementById("quantity").value;

            //form won't be sent unless the quantity number is greater than 0
            if (quantity <= 0) {

                alert("Quantity number must greater than 0!");

                return false;

            } else {

                return true;

            }

        }
		
		//quantity number minus 1 function
        function minus() {
			var price=document.getElementById("price_input").value;
			//get current quantity number
            var quantity = document.getElementById("quantity").value;
			
			//quantity number must greater than 0
            if (quantity <= 0) {

                alert("Quantity number must greater than 0!");

            } else {
				
				//minus 1 to current quantity number
                quantity--;
                if(quantity!=0){
                    var total_price=price*quantity;
                    document.getElementById("price").innerHTML = "Price: " + total_price;
                }
		      
		        
				//update quantity number
                document.getElementById("quantity").value = quantity;
           

            }

        }
		
		//quantity number plus 1 function
        function plus() {
			
			//get current quantity number
            var quantity = document.getElementById("quantity").value;
			var price=document.getElementById("price_input").value;
			//add 1 to current quantity number
            quantity++;
			var total_price=price*quantity;
			//update quantity number
            document.getElementById("quantity").value = quantity;
            document.getElementById("price").innerHTML = "Price: " + total_price;

        }
       
        function changeType(){

            if(document.getElementById("option").value=="$option1"){
            
                document.getElementById("price").innerHTML = "Price: $" + $op1Price;
                document.getElementById("price_input").value=$op1Price;
                console.log(document.getElementById("price_input").value);
                
            }else{
            
                document.getElementById("price").innerHTML = "Price: $" + $op2Price;
                 document.getElementById("price_input").value=$op2Price;
                 console.log(document.getElementById("price_input").value);
       
            }
        
        }
        


    </script>

       

            <section id="SGN9detail">

                <article>
                    <h1>$name</h1>
					<!-- Original image below sourced for educational purposes: https://www.jbhifi.com.au/phones/Outright-Mobile-Handsets/samsung/samsung-galaxy-note9-512gb-midnight-black/329672/-->
                    <img class="imgfloatLeft" src="$picture" alt="img1">

                    <form action='' method="post" onsubmit="return validateForm()">
                        <fieldset>
                            <legend><em><b>Products Purshased</b></em></legend>
                            <p id="price">Price: $%1.2f</p>
                            <input type="hidden" id="price_input" name="price" value="$op1Price">
                            <input type="hidden" id="service code" name="id" value="$id"> Quantity:
                            <input type="hidden" id="name" name="name" value="$name">
                            <input type="hidden" id="picture" name="picture" value="$picture">
                            <br>

                            <input type="button" value="-" onclick="minus()" />
                            <input type="text" name="quantity" id="quantity" value="0" />
                            <input type="button" value="+" onclick="plus()" />
                            <br><br>

                            <p>Choose your colour:
                                <p>
                           <select id="option" name="option" onchange="changeType()">
                                <option value="$option1" >$option1</option>
                                <option value="$option2" >$option2</option>
                            </select>

                                    <br><br><br>
                                    <input type="submit" value="Add to Cart" />
                        </fieldset>
                    </form>
                    </br>
                    <p>$description</p>

                    
                    
                    <br><br>
					
                   
                </article>
            </section>
        </article>
OUTPUT;
printf($form,$op1Price);;
?>        
   <?php end_module();?>

</html>
